/*****************************************************************************
  Copyright ?2002-2004 by Martin Cook. All rights are reserved. If you like
  this code then feel free to go ahead and use it. The only thing I ask is 
  that you don't remove or alter my copyright notice. Your use of this 
  software is entirely at your own risk. I make no claims about the 
  reliability or fitness of this code for any particular purpose. If you 
  make changes or additions to this code then please clearly mark your code 
  as yours. If you have questions or comments then please contact me at: 
  martin@codegator.com
  
  Have Fun! :o)
*****************************************************************************/


using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Runtime.InteropServices;

using Microsoft.ApplicationBlocks.Data.OleDb;

using CG.Security.Configuration;
using CG.Security.Data.Configuration;

namespace CG.Security.Data.Access
{

	/// <summary>
	/// Provides static methods that supply helper utilities for accessing 
	/// identity (autonumber) values for Access databases.
	/// </summary>
	internal sealed class IdentityHelper
	{

		// ******************************************************************
		// Public methods.
		// ******************************************************************

		#region Public methods

		/// <summary>
		/// Used to retrieve the last autonumber value for the specified table.
		/// </summary>
		/// <param name="conn">The database connection that was used to perform
		/// the last change to the table.</param>
		/// <param name="tableName">The name of te table.</param>
		/// <returns>The last autonumber value used.</returns>
		public static int GetLastValue(
			OleDbConnection conn, 
			string tableName
			)
		{

			// Execute the SQL statement.
			return (Int32)OleDbHelper.ExecuteScalar(
				conn,
				CommandType.Text, 
				"SELECT @@IDENTITY FROM " + tableName
				);

		} // End GetLastValue()

		// ******************************************************************

		/// <summary>
		/// Used to retrieve the last autonumber value for the specified table.
		/// </summary>
		/// <param name="conn">The database transaction that was used to perform
		/// the last change to the table.</param>
		/// <param name="tableName">The name of te table.</param>
		/// <returns>The last autonumber value used.</returns>
		public static int GetLastValue(OleDbTransaction txn, string tableName)
		{

			// Execute the SQL statement.
			return (Int32)OleDbHelper.ExecuteScalar(
				txn,
				CommandType.Text, 
				"SELECT @@IDENTITY FROM " + tableName
				);

		} // End GetLastValue()

		#endregion

	} // End class IdentityHelper

} // End namespace CG.Security.Data.Access
